# EMF-style global quarterly pulse. CK2 decisions are evaluated in character scope,
# so independent AI rulers act only as candidates for the scheduler. The global
# 45-day pulse flag makes the first successful candidate lock out every other ruler.
# Weather itself is executed from that ruler's capital province so all existing
# Carnal Realms province/title scripted effects retain their expected scope.

decisions = {
	cr_weather_maintenance = {
		only_independent = yes
		is_high_prio = yes

		potential = {
			ai = yes
			is_playable = yes
			independent = yes
			has_game_rule = { name = cr_weather value = enabled }
			capital_scope = { has_owner = yes }

			# Directly follows EMF's quarterly debounce architecture.
			OR = {
				NOT = { has_global_flag = cr_weather_quarter_pulse }
				had_global_flag = { flag = cr_weather_quarter_pulse days = 45 }
			}

			# Fire only in December, March, June, or September.
			OR = {
				month = 11
				AND = {
					month = 2
					NOT = { month = 3 }
				}
				AND = {
					month = 5
					NOT = { month = 6 }
				}
				AND = {
					month = 8
					NOT = { month = 9 }
				}
			}
		}

		allow = { }

		effect = {
			clr_global_flag = cr_weather_quarter_pulse
			set_global_flag = cr_weather_quarter_pulse
			set_global_flag = cr_weather_initialized
			export_to_variable = { which = global_cr_weather_year value = year }

			# Remove any stale state left by the superseded controller implementation.
			clr_global_flag = cr_weather_bootstrap_pending
			clr_global_flag = cr_weather_restart_clock
			clr_global_flag = cr_weather_force_rebuild
			clr_global_flag = cr_weather_clock_started
			clr_global_flag = cr_weather_heartbeat
			clr_global_flag = cr_weather_maintenance_pulse

			capital_scope = {
				if = {
					limit = { month = 11 }
					clr_global_flag = cr_weather_season_spring
					clr_global_flag = cr_weather_season_summer
					clr_global_flag = cr_weather_season_autumn
					set_global_flag = cr_weather_season_winter
					# Only December invalidates the annual kingdom plan. The next
					# three quarterly passes reuse its stored spring/summer/autumn rolls.
					cr_clear_territorial_weather_plans_effect = yes
				}
				else_if = {
					limit = { month = 2 NOT = { month = 3 } }
					clr_global_flag = cr_weather_season_winter
					clr_global_flag = cr_weather_season_summer
					clr_global_flag = cr_weather_season_autumn
					set_global_flag = cr_weather_season_spring
				}
				else_if = {
					limit = { month = 5 NOT = { month = 6 } }
					clr_global_flag = cr_weather_season_winter
					clr_global_flag = cr_weather_season_spring
					clr_global_flag = cr_weather_season_autumn
					set_global_flag = cr_weather_season_summer
				}
				else = {
					clr_global_flag = cr_weather_season_winter
					clr_global_flag = cr_weather_season_spring
					clr_global_flag = cr_weather_season_summer
					set_global_flag = cr_weather_season_autumn
				}

				cr_update_world_weather_effect = yes
			}
		}

		ai_will_do = { factor = 100 }
	}
}
